var express = require("express");
var router = express.Router();

const { htmlParseCatalogs, getBookPageContent } = require("../utils/index");
const {
	searchBook,
	getCatalogs,
	getPageContent,
	getTopBook,
} = require("../utils/parser");

router.get("/", function (req, res, next) {
	res.send("respond with ");
});

router.post("/parse", async (req, res) => {
	let params = req.body;
	const content = await htmlParseCatalogs(params.origin);
	if (content) {
		res.ok(content);
	} else {
		res.error("解析失败", 0);
	}
});

router.post("/getPage", async (req, res) => {
	let params = req.body;
	const content = await getBookPageContent(params);
	if (content) {
		res.ok(content);
	} else {
		res.error("解析失败", 0);
	}
});

router.post("/search", async (req, res) => {
	let params = req.body;

	const content = await searchBook(params);
	if (content) {
		res.ok(content);
	} else {
		res.error("解析失败", 0);
	}
});

router.post("/getCatalogs", async (req, res) => {
	let params = req.body;

	const content = await getCatalogs(params);
	if (content) {
		res.ok(content);
	} else {
		res.error("解析失败", 0);
	}
});

router.post("/getContent", async (req, res) => {
	let params = req.body;

	const content = await getPageContent(params);
	if (content) {
		res.ok(content);
	} else {
		res.error("解析失败", 0);
	}
});

router.post("/getTopBooks", async (req, res) => {
	let params = req.body;
	const content = await getTopBook(params);
	if (content) {
		res.ok(content);
	} else {
		res.error("解析失败", 0);
	}
});

module.exports = router;
